<?php

/**
 * 评价操作
 */

namespace app\order\common;

class CommentCommon extends \app\member\common\MemberCommon {

    private $orderInfo = [];

    public function __construct() {
        parent::__construct();
    }

    protected function push() {

        $id = request('', 'id');
        $info = target('order/OrderGoods')->getWhereInfo([
            'id' => $id
        ]);
        if (empty($info)) {
            $this->error('该订单商品不存在');
        }
        if ($info['comment_status']) {
            $this->error('该商品已评价!');
        }

        $target = target('order/Order');
        $orderInfo = $target->getInfo($info['order_id']);

        if ($orderInfo['order_user_id'] <> $this->userInfo['user_id']) {
            $this->error('该订单无法操作!');
        }

        if($orderInfo['status_data']['action'] <> 'comment') {
            $this->error('暂时无法评价该订单!');
        }

        if (!isPost()) {

            $this->pageInfo['name'] = '商品评价';
            $this->setMeta('商品评价');
            $this->setCrumb([
                [
                    'name' => '商品评价',
                    'url' => url('', ['id' => $id])
                ]
            ]);

            $this->assign('id', $id);
            $this->assign('info', $info);
            $this->assign('orderInfo', $orderInfo);
            $this->memberDisplay();
        } else {
            $level = request('post', 'level', 2, 'intval');
            $content = str_len(request('post', 'content', '', 'html_clear'), 300);
            if (empty($content)) {
                $this->error('请填写评价内容!');
            }
            $images = request('post', 'images');
            if (!empty($images) && is_array($images)) {
                $httpHost = 'http://' . trim($_SERVER["HTTP_HOST"], '/');
                foreach ($images as $image) {
                    if (substr($image, 0, 1) <> '/' && strpos($image, $httpHost, 0) === false) {
                        $this->error('您上传的图片有误,请重新上传!');
                    }
                }
            }else {
                $images = [];
            }
            $images = $images ? $images : [];
            $spec = [];
            if (!empty($info['options'])) {
                foreach ($info['options'] as $vo) {
                    $spec[] = $vo['value'];
                }
            }
            target('order/OrderComment')->beginTransaction();
            $status = target('order/OrderComment')->add([
                'order_goods_id' => $info['id'],
                'user_id' => $this->userInfo['user_id'],
                'app' => $orderInfo['order_app'],
                'has_id' => $info['goods_id'],
                'spec' => implode(' ', $spec),
                'time' => time(),
                'content' => $content,
                'level' => $level,
                'images' => serialize($images)
            ]);
            if (!$status) {
                target('order/OrderComment')->rollBack();
                $this->error('商品评价失败,请稍后再试!');
            }
            $status = target('order/OrderGoods')->edit([
                'id' => $id,
                'comment_status' => 1
            ]);
            if (!$status) {
                target('order/OrderComment')->rollBack();
                $this->error('商品评价失败,请稍后再试!');
            }
            $count = target('order/OrderGoods')->countList([
                'order_id' => $orderInfo['order_id'],
                '_sql' => 'id <> ' . $id,
                'comment_status' => 0
            ]);
            if (!$count) {
                $status = target('order/Order')->edit([
                    'order_id' => $orderInfo['order_id'],
                    'comment_status' => 1
                ]);
                if (!$status) {
                    target('order/OrderComment')->rollBack();
                    $this->error('商品评价失败,请稍后再试!');
                }
            }
            $status = target('shop/Shop')->where(['shop_id' => $info['goods_id']])->setInc('comments', 1);
            if (!$status) {
                target('order/OrderComment')->rollBack();
                $this->error('商品评价失败,请稍后再试!');
            }
            $status = target('shop/Shop')->where(['shop_id' => $info['goods_id']])->setInc('score', $level);
            if (!$status) {
                target('order/OrderComment')->rollBack();
                $this->error('商品评价失败,请稍后再试!');
            }
            target('order/OrderComment')->commit();
            $this->success('商品评价成功!', $this->action);
        }

    }


}